import React from 'react'
import { useModel } from '@umijs/max'
import { Drawer, message } from 'antd'
import { listMyTableInfoByPage } from '@/services/tableInfoService'
import TableInfoCard from '../TableInfoCard'

interface Props {
  onImport: (values: TableInfoType.TableInfo) => void;
  visible: boolean;
  onClose: () => void;
}

/**
 * 导入表抽屉
 * @constructor
 */
const ImportTableDrawer: React.FC<Props> = (props) => {
  const { visible, onImport, onClose } = props
  const { initialState } = useModel('@@initialState')
  const loginUser = initialState?.loginUser

  /**
   * 记载我的数据
   * @param searchParams
   * @param setDataList
   * @param setTotal
   */
  const loadMyData = loginUser
    ? (
      searchParams: TableInfoType.TableInfoQueryRequest,
      setDataList: (dataList: TableInfoType.TableInfo[]) => void,
      setTotal: (total: number) => void
    ) => {
      listMyTableInfoByPage(searchParams)
        .then((res) => {
          setDataList(res.data.records)
          setTotal(res.data.total)
        })
        .catch((e) => {
          message.error('加载失败，' + e.message)
        })
    }
    : undefined
  return (
    <Drawer
      title='导入表'
      contentWrapperStyle={{ width: '60%', minWidth: 320 }}
      open={visible}
      onClose={onClose}
    >
      <TableInfoCard onLoad={loadMyData} onImport={onImport} />
    </Drawer>
  )
}

export default ImportTableDrawer